#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;

int num(char a,char b){
	if(a == 'A'  &&  b == 'A')
		return 5;
	if(a == 'C'  &&  b == 'C')
		return 5;
	if(a == 'G'  &&  b == 'G')
		return 5;
	if(a == 'T'  &&  b == 'T')
		return 5;
	if((a == 'A'  &&  b == 'C')  ||  (b == 'A'  &&  a == 'C'))
		return -1;
	if((a == 'A'  &&  b == 'G')  ||  (b == 'A'  &&  a == 'G'))
		return -2;
	if((a == 'A'  &&  b == 'T')  ||  (b == 'A'  &&  a == 'T'))
		return -1;
	if((a == 'A'  &&  b == '-')  ||  (b == 'A'  &&  a == '-'))
		return -3;
	if((a == 'C'  &&  b == 'G')  ||  (b == 'C'  &&  a == 'G'))
		return -3;
	if((a == 'C'  &&  b == 'T')  ||  (b == 'C'  &&  a == 'T'))
		return -2;
	if((a == 'C'  &&  b == '-')  ||  (b == 'C'  &&  a == '-'))
		return -4;
	if((a == 'G'  &&  b == 'T')  ||  (b == 'G'  &&  a == 'T'))
		return -2;
	if((a == 'G'  &&  b == '-')  ||  (b == 'G'  &&  a == '-'))
		return -2;
	if((a == 'T'  &&  b == '-')  ||  (b == 'T'  &&  a == '-'))
		return -1;
	return 0;
}

int main(){
	int al,bl;
	string a,b;
	cin>>al;
	cin>>a;
	cin>>bl;
	cin>>b;
	int dp[al+1][bl+1];
	dp[0][0] = 0;
	for(int i=1;i<=al;i++)
		dp[i][0] = dp[i-1][0]+num('-',a[i-1]);
	for(int i=1;i<=bl;i++)
		dp[0][i] = dp[0][i-1]+num('-',b[i-1]);
//	memset(dp,0,sizeof(dp));
	for(int i=1;i<=al;i++){
		for(int j=1;j<=bl;j++){
			dp[i][j] = max(dp[i-1][j-1]+num(a[i-1],b[j-1]),max(dp[i-1][j]+num(a[i-1],'-'),dp[i][j-1]+num('-',b[j-1])));
//			cout<<dp[i][j]<<" ";
		}
//		cout<<endl;
	}
	cout<<dp[al][bl];
	return 0;
}
